package com.wanmait.gohome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.mapper.CommentMapper;
import com.wanmait.gohome.mapper.ConfigMapper;
import com.wanmait.gohome.pojo.Comment;
import com.wanmait.gohome.pojo.Config;
import com.wanmait.gohome.pojo.Journal;
import com.wanmait.gohome.service.CommentService;
import com.wanmait.gohome.service.CommonService;
import com.wanmait.gohome.util.SysContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ConfigMapper configMapper;



    @Override
    public void insert(Comment comment) {
        commentMapper.insert(comment);
    }

    @Override
    public void update(Integer id) {

    }

    @Override
    public void delete(Integer id) {
    commentMapper.deleteComment(id);
    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public List<Comment> findAll() {
        return null;
    }

    @Override
    public Comment findById(Integer id) {
        return null;
    }

    @Override
    public PageInfo<Comment> findPage() {
        int pageNum = SysContext.getPageNum();
        int pageSize=0;

        try {
            Config config = configMapper.selectManagePageInteger();
            pageSize = Integer.parseInt(config.getConfigValue());//数据库里取
        } catch (NumberFormatException e) {
            pageSize=10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> comments  = commentMapper.findAll();
        PageInfo<Comment> pageInfo = new PageInfo<>(comments, 7);
        return pageInfo;
    }

    @Override
    public List<Comment> findAll(Comment object) {
        return null;
    }

    @Override
    public PageInfo<Comment> findPage(Comment Object) {
        return null;
    }

    @Override
    public PageInfo<Comment> findAllById(int id) {
        int pageNum = SysContext.getPageNum();
        int pageSize = 5;
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> comments = (List<Comment>) commentMapper.findAlls(id);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments, 3);
        return pageInfo;
    }

    @Override
    public void updateEnable(Comment comment) {
        commentMapper.updateEnable(comment);
    }

    @Override
    public PageInfo<Comment> findSealComment() {
        int pageNum = SysContext.getPageNum();
        int pageSize=0;

        try {
            Config config = configMapper.selectManagePageInteger();
            pageSize = Integer.parseInt(config.getConfigValue());//数据库里取
        } catch (NumberFormatException e) {
            pageSize=10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> comments  = commentMapper.findSealComment();
        PageInfo<Comment> pageInfo = new PageInfo<>(comments, 7);
        return pageInfo;
    }


}
